﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;

namespace ZLCMS.Web.SystemManage.Basics.MessageBoard
{
    public partial class MessageList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this) { RecordCount = GetPageCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                BindPageData(_framePage.PageIndex, _framePage.PageSize);
            }
            else 
            {
                BindPageData(1, 20);
            }
        }

        public string GetMessageReply(int messageId)
        {
            StringBuilder tmpBuilder = new StringBuilder();

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IList<MessageReply> messageReplyList = session.CreateCriteria<MessageReply>().Add(Expression.Eq("Messageid", messageId)).List<MessageReply>();

                if (messageReplyList.Count == 0)
                {
                    return "[暂未回复]";
                }
                else
                {
                    foreach (MessageReply tmp in messageReplyList)
                    {
                        //tmpBuilder.Append("<tr><td>" + tmp.Content + "</td></tr>");
                        tmpBuilder.Append("<span>" + tmp.Content + "</span></br>");
                    }
                }
            }
            return tmpBuilder.ToString();
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindPageData(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<MessageReply>();
                criteria.AddOrder(Order.Desc("Addtime")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                SetCondition(criteria);

                gv_default.DataSource = criteria.List();
                gv_default.DataBind();
            }
        }

        private void SetCondition(ICriteria criteria)
        {
            if (tb_search_title.Text.Trim() != "")
            {
                criteria.Add(Expression.Like("Title", "%" + tb_search_title.Text + "%"));
            }

            if (tb_search_content.Text.Trim() != "")
            {
                criteria.Add(Expression.Like("Content", "%" + tb_search_content.Text + "%"));
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetPageCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria ctr = session.CreateCriteria<MessageReply>();
                SetCondition(ctr);
                return (int)ctr.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }
    }
}
